package com.redijedi.tapestry.internal.datefield;

import org.apache.tapestry.OptionModel;

import com.redijedi.tapestry.util.OptionModelImpl;

/**
 * Helper methods for date related tasks.
 * 
 * @author torr
 * 
 */
public class DateUtils {

	private final static int _maxDaysInMonth = 31;

	private final static int _maxMonthsInYear = 12;

	private final static String[] _monthNames = { "January", "February",
			"March", "April", "May", "June", "July", "August", "September",
			"October", "November", "December" };

	private static OptionModel[] _days;

	private static OptionModel[] _months;

	public static OptionModel[] getDayOptionModels() {
		if (_days == null) {
			_days = new OptionModel[_maxDaysInMonth];
			for (int i = 0; i < _maxDaysInMonth; i++) {
				_days[i] = new OptionModelImpl(i + 1, i + 1);
			}
		}
		return _days;
	}

	public static OptionModel[] getMonthOptionModels() {
		if (_months == null) {
			_months = new OptionModel[_maxMonthsInYear];
			for (int i = 0; i < _maxMonthsInYear; i++) {
				_months[i] = new OptionModelImpl(i, _monthNames[i]);
			}
		}
		return _months;
	}

	public static OptionModel[] getYearOptionModels(int from, int to) {
		OptionModel[] years = new OptionModel[to - from + 1];
		for (int i = from, j = 0; i <= to; i++, j++) {
			years[j] = new OptionModelImpl(i, i);
		}
		return years;
	}

}
